package com.mashuai.utils.ztest.toolkit.msDataSource;

import com.mashuai.utils.toolkit.core.msDataSource.MsDataSourceUtils;
import com.mashuai.utils.toolkit.core.msMp.MsDb;
import com.mashuai.utils.ztest.pb.bean.MsUser;
import com.mashuai.utils.ztest.pb.mapper.MsUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: MsDataSourceController
 * @Author : mashuai
 * @Description: TODO
 */
@RequestMapping("/dataSource")
@RestController
public class MsDataSourceController {

    @Autowired
    private MsUserMapper msUserMapper;

    /**
     * 演示如何通过@MsDataSource注解自动切换数据源
     **/
    @GetMapping("/changeDataBaseAnnotation")
    public Object changeDataBaseAnno() {
        //查询默认的数据源，默认查10.88.15.37主库
        List<MsUser> list1 = msUserMapper.getDefDataSourceData();
        System.err.println(list1.stream().map(t -> t.getName()).collect(Collectors.joining(",")));

        //切换到10.44.2.3库
        List<MsUser> list2 = msUserMapper.annotationChangeDataSourceDb2();
        System.err.println(list2.stream().map(t -> t.getName()).collect(Collectors.joining(",")));

        //在查询主库数据
        List<MsUser> list11 = msUserMapper.getDefDataSourceData();
        System.err.println(list11.stream().map(t -> t.getName()).collect(Collectors.joining(",")));
        return null;
    }

    /**
     * 演示通过 MsDataSourceUtils.addDataSource()方法，模拟从主库的A表中读取从库C的数据源进行查询
     **/
    @GetMapping("/changeDataBase")
    public Object changeDataBase() {
        //查询默认的数据源，默认查10.88.15.37主库
        List<MsUser> list1 = msUserMapper.getDefDataSourceData();
        System.err.println(list1.stream().map(t -> t.getName()).collect(Collectors.joining(",")));

        //切换到10.44.2.3库
        List<MsUser> list2 = msUserMapper.annotationChangeDataSourceDb2();
        System.err.println(list2.stream().map(t -> t.getName()).collect(Collectors.joining(",")));

        //手动切换到10.88.15.26库
        //模拟：在请求时候，需要从默认数据库A的t表获取一个数据源Db3，然后动态去加载该数据源
        List<MsUser> list3 = MsDataSourceUtils.switchDataSource("db3",
                "jdbc:mysql://10.88.15.26:3306/syncplant_ms?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true",
                "root",
                "root", () -> msUserMapper.manualChangeDataSourceDef());
        System.err.println(list3.stream().map(t -> t.getName()).collect(Collectors.joining(",")));

        //在查询主库数据
        List<MsUser> list11 = MsDb.list(null, MsUser.class);
        System.err.println(list11.stream().map(t -> t.getName()).collect(Collectors.joining(",")));
        return null;
    }

    /**
     * 演示如何替换当前默认的主数据源
     **/
    @GetMapping("/setDefDataSource")
    public void setDefDataSource() {
        List<MsUser> list1 = msUserMapper.getDefDataSourceData();
        System.err.println(list1.stream().map(t -> t.getName()).collect(Collectors.joining(",")));

        //设置10.88.15.26库为主库
        MsDataSourceUtils.setDefDataSource(
                "jdbc:mysql://10.88.15.26:3306/syncplant_ms?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true",
                "root",
                "root");
        List<MsUser> list = MsDb.list(MsUser.class);
        System.err.println(list.stream().map(t -> t.getName()).collect(Collectors.joining(",")));

        List<MsUser> list22 = MsDb.list(MsUser.class);
        System.err.println(list22.stream().map(t -> t.getName()).collect(Collectors.joining(",")));

        List<MsUser> list33 = MsDb.list(MsUser.class);
        System.err.println(list33.stream().map(t -> t.getName()).collect(Collectors.joining(",")));
//        //切换到10.44.2.3库
//        List<MsUser> list2 = msUserMapper.annotationChangeDataSourceDb2();
//        System.err.println(list2.stream().map(t -> t.getName()).collect(Collectors.joining(",")));
//
//        //在查询主库数据
//        List<MsUser> list11 = MsDb.list(null, MsUser.class);
//        System.err.println(list11.stream().map(t -> t.getName()).collect(Collectors.joining(",")));
    }
}
